package JUC;

import org.springframework.core.task.support.ExecutorServiceAdapter;

import java.util.concurrent.*;

public class CompletableFutureWithThreadPollDemo {
    public static void main(String[] args) {
        ExecutorService threadPool= Executors.newFixedThreadPool(5);
        try {
            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
                try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}
                System.out.println("1号任务"+Thread.currentThread().getName());
                return "abcd";
            },threadPool).thenRunAsync(() -> {
                try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}
                System.out.println("2号任务"+Thread.currentThread().getName());
            }).thenRunAsync(() -> {
                try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}
                System.out.println("3号任务"+Thread.currentThread().getName());
            }).thenRunAsync(() -> {
                try {TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}
                System.out.println("4号任务"+Thread.currentThread().getName());
            }).thenRunAsync(() -> {
                try {TimeUnit.MILLISECONDS.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}
                System.out.println("5号任务"+Thread.currentThread().getName());
            });

            System.out.println(voidCompletableFuture.get(2L, TimeUnit.SECONDS));
        }catch (Exception e){

        }
    }
}
